from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView,ListCreateAPIView,RetrieveAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin,RetrieveModelMixin
from rest_framework.viewsets import ViewSet,ModelViewSet
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import action

from .models import BookInfo, HeroInfo
from .serializers import BookInfoModelSerializer  #序列化器

#利用：GenericViewSet视图集实现查询单一和所有数据接口
class BookViewSet(ModelViewSet):
    # 指定序列化器
    serializer_class = BookInfoModelSerializer
    # 指定查询集
    queryset = BookInfo.objects.all()

    #IsAuthenticated 表示只有通过认证（登录用户）的用户才能访问此类视图中的所有接口
    # permission_classes = [IsAuthenticated]
    # def get_queryset(self):
    #     if self.action == 'latest':
    #         return BookInfo.objects.all()
    #     else:
    #         return HeroInfo.objects.all()

    # /books/latest/
    @action(methods=['get'], detail=False)
    def latest(self, request):
        """获取最后一本图书"""
        book = BookInfo.objects.latest('id')
        serializer = self.get_serializer(book)
        return Response(serializer.data)


    @action(methods=['put'], detail=True)
    def update_read(self, request, pk):
        """修改图书的阅读量数据"""
        book = self.get_object()
        book.bread = request.data.get('bread')
        book.save()
        serializer = self.get_serializer(book)
        return Response(serializer.data)


#------------------------------------------------------------------

# 利用:ViewSet 视图集实现查询单一和所有数据接口
# class BookViewSet(ViewSet):
#     def list(self, request):
#         qs = BookInfo.objects.all()
#         serializer = BookInfoModelSerializer(qs, many=True)
#         return Response(serializer.data)
#
#     def retrieve(self, request, pk):
#         try:
#             book = BookInfo.objects.get(id=pk)
#         except BookInfo.DoesNotExist:
#             return Response(status=status.HTTP_404_NOT_FOUND)
#
#         serializer = BookInfoModelSerializer(book)
#         return Response(serializer.data)

# ------------------------------------------------------------

# # 利用GenericAPIView + mixin的合成子类来实现标准接口
# class BookListView(ListCreateAPIView):
#     """列表视图"""
#     # 1. 指定序列化器类
#     serializer_class = BookInfoModelSerializer
#     # 2. 指定查询集
#     queryset = BookInfo.objects.all()
#
#         # def get(self, request, *args, **kwargs):
#         #     return self.list(request)
#
# class BookDetailView(RetrieveAPIView):
#     """详细视图"""
#     # 1. 指定视图序列化器
#     serializer_class = BookInfoModelSerializer
#     # 2. 指定查询集
#     queryset = BookInfo.objects.all()


# -------------------------------------------------------------------------------

# 利用GenericAPIView + minins 扩展来实现获取所有图书和单一图书接口

# class BookListView(CreateModelMixin,ListModelMixin,GenericAPIView):
#     """列表视图"""
#     # 1. 指定序列化器类
#     serializer_class = BookInfoModelSerializer
#     # 2. 指定查询集
#     queryset = BookInfo.objects.all()
#
#     def get(self, request):
#         # 1. 获取查询集
#         # qs = self.get_queryset()
#         # # 2. 创建序列化器对象进行序列化操作
#         # serializer = self.get_serializer(qs, many=True)
#         # # 3.响应
#         # return Response(serializer.data)
#         return self.list(request)
#
#     def post(self, request):
#         return self.create(request)
#
# class BookDetailView(RetrieveModelMixin, GenericAPIView):
#     """详细视图"""
#     # 1. 指定序列化器类
#     serializer_class = BookInfoModelSerializer
#     # 2. 指定查询集
#     queryset = BookInfo.objects.all()
#
#     def get(self, request, pk):
#         # # 1. 获取到要查询的模型对象
#         # book = self.get_object()
#         # # 2. 创建序列化器进行序列化
#         # serializer = self.get_serializer(book)
#         # # 3.响应
#         # return Response(serializer.data)
#
#         return self.retrieve(request)

# ------------------------------------------------------------------------
# # 利用GenericAPIView 实现查询所有图书及查询指定单一图书两个接口
#
# class BookListView(GenericAPIView):
#     """列表视图"""
#     # 1. 指定序列化器类
#     serializer_class = BookInfoModelSerializer
#     # 2. 指定查询集
#     queryset = BookInfo.objects.all()
#
#     def get(self, request):
#         # 1. 获取查询集
#         qs = self.get_queryset()
#         # 2. 创建序列化器对象进行序列化器操作
#         serializer = self.get_serializer(qs, many=True)
#         # 3. 响应
#         return Response(serializer.data)
# #
#
#
#
# class BookDetailView(GenericAPIView):
#     # 1. 指定序列化器类
#     serializer_class = BookInfoModelSerializer
#     # 2. 指定查询集
#     queryset = BookInfo.objects.all()
#
#     """详情视图"""
#     def get(self, request, pk):
#         # 1. 获取到要查询的模型对象
#         book = self.get_object()
#         # 2. 创建序列化器进行序列化
#         serializer = self.get_serializer(book)
#         # 3. 响应
#
#         return Response(serializer.data)



#-----------------------------------------------------------------------

# 利用DRF中的APIView 实现获取所有图书接口
# GET /books/
class BookListView(APIView):

    def get(self, request):
        # 1. 获取出查询集
        queryset1 = BookInfo.objects.all()
        # 2. 创建序列化器对象进行序列化
        serializer = BookInfoModelSerializer(queryset1, many=True) #此时查询出来的是查询集，所以后面的参数为many=True
        # 3.响应 Response
        return Response(serializer.data)

    def post(self, request):
        # 1.获取请求的数据
        data = request.data
        # 2.创建序列化器对象进行反序列化
        serializer = BookInfoModelSerializer(data=data)
        # 3.调用检验方法数据的校验
        serializer.is_valid(raise_exception=True)
        #4.保存数据到数据库
        serializer.save()
        #5.响应
        return Response(serializer.data)

class BookDetailView(APIView):
    """详情视图"""
    def put(self, request, pk):
        # 0.获取前段的参数，在数据库查询出来
        # 1.获取请求数据
        data = request.data
        # 2.创建序列化器对象反序列化
        serializer = BookInfoModelSerializer()
        # 3. 调用校验方法
        # 4. 调用序列化器的save方法保存数据
        #5.响应
